<?php
namespace Admin\Controller;
use Common\Model\FollowModel;

use Think\Controller;
use Admin\Controller\AdminController; 
use Think\Verify;

class ComPayController extends AdminController{
	
	public function index(){
		$id=I('get.id');
		$where['id']=array('eq',$id);
		$with=M('futuresWithdrawals')->field('id,money,handling_fee,user_id,bank_id,compay,remark')->where($where)->find();				
		//dump($with);
		$user = M('Follow')->field('id,money,nickname,openid,blocked_fund')->where(array('id'=>$with['user_id']))->find();
		//dump($user);
		//搜索用户银行卡
		$realname = M('futuresUserBank')->where(array('user_id'=>$user['id'],'id'=>$with['bank_id']))->getField('bank_realname');
		$user['realname'] = $realname;
		$this->assign('user',$user); //用户信息
		$this->assign('with',$with); //提现信息
		$this->display();

	}

	/*页面加载*/
	public function pay(){

		//获取id和密码
		$id = I('post.withid');
		$password = I('post.password');
		//判断密码是否正确
		if($password!='lg198521'){
			$this->error('密码不正确');
		}
		//根据id 获取提现数据
		$where['id']=array('eq',$id);
		$with=M('futuresWithdrawals')->field('id,money,handling_fee,user_id,bank_id,compay,partner_trade_no')->where($where)->find();				
		if(!$with){
			$this->error('没有相关数据！');
		}

		if($with['compay']==1 || $with['is_pay']==1){
			$this->error('该提现已确认付款，不能重复付款！');
		}
		//dump($with);
		$user = M('Follow')->field('id,money,nickname,openid,blocked_fund')->where(array('id'=>$with['user_id']))->find();
		//dump($user);
		//搜索用户银行卡
		$realname = M('FuturesUserBank')->where(array('user_id'=>$user['id'],'id'=>$with['bank_id']))->getField('bank_realname');
		$user['realname'] = $realname;

		//如果冻结资金小于提现金额，则数据肯定有问题
		if($user['blocked_fund']<$with['money']){
			$this->error('非法确认！');
		}

		//确认后的金额
		$follow['blocked_fund'] = $user['blocked_fund']-$with['money']-$with['handling_fee'];
		$data['pay_time'] = time();				//确认时间
		$data['is_pay']=1;						//修改状态
		$data['compay']=1;
		$data['remark']="企业付款确认提现金额".$money."元";		//说明

		if(M('futuresWithdrawals')->where($where)->save($data)===false){
			$this->error('确认失败');
		}else{
			//if($with['compay']==0 && $with['is_pay']==0){
			M('follow')->where('id='.$user['id'])->save($follow);		//修改冻结资金
			//}
			import('Futures.WxPay.lib.ComPay');
			$pay = new \ComPay($user['openid'],$with['partner_trade_no']);
			//dump($pay);
			$pay->setAmount($with['money']*100); //设置金额1分
			$pay->setDesc("提现付款");	//描述
			$pay->setOpenid($user['openid']);	//用户Openid
			$pay->setUserName($realname);
			//var_dump($pay);
			$xml = $pay->ComPay();
			//dump($xml);
			$res=simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
			//dump($res);
			if(!$res || $res->result_code == 'FAIL' || !empty($res->err_code)){//失败的情况
				//返回提示信息 $res->return_msg;
				$msg = $res->return_msg;
				M('FuturesWithdrawals')->where($where)->save(array('is_pay'=>0,'compay'=>2,'remark'=>$msg.'(付款失败,需补发)'));
				//返回冻结金额
				$backfollow['blocked_fund'] = $user['blocked_fund'];
				M('follow')->where('id='.$user['id'])->save($backfollow);
				$this->error($msg.'(付款失败,需补发)');
			}else{
				$partner_trade_no =$res->partner_trade_no.'';
				$payment_no =$res->payment_no.'';
				$payment_time = $res->payment_time.'';
	
				M('FuturesWithdrawals')->where($where)->save(array('partner_trade_no'=>$partner_trade_no,'payment_no'=>$payment_no,'payment_time'=>$payment_time));
				//echo M('FuturesWithdrawals')->getLastSql();
				//$this->success($rr,U('FuturesDraw/index'));
				$this->success('付款成功',U('FuturesDraw/index'));
			}
			
		}

	}
}
